TOOL.Category = "Weapons"
TOOL.Name = "#Naquadah Bomb"
TOOL.Command = nil
TOOL.ConfigName = ""
TOOL.Tab = "Stargate"
TOOL.AddToMenu = false -- Tell GMod not to add this STool. aVoN's code will do it later.

local entityName = "naquadah_bomb"

TOOL.ClientConVar['detonationCode'] = ""
TOOL.ClientConVar['abortCode'] = ""
TOOL.ClientConVar['yield'] = "100"
TOOL.ClientConVar['chargeTime'] = "4"

cleanup.Register(entityName)

if(CLIENT) then
	language.Add("Tool_"..entityName.."_name", 'Naquadah Bomb')
	language.Add("Tool_"..entityName.."_desc", 'Creates a naquadah-powered explosive device.')
	language.Add("Tool_"..entityName.."_0", 'Left click to create the bomb.')
	language.Add("Undone_"..entityName, 'Naquadah Bomb Undone')
	language.Add("Cleanup_"..entityName, 'Naquadah Bombs')
	language.Add("Cleaned_"..entityName, 'Removed all naquadah bombs')
	language.Add("SBoxLimit_"..entityName, 'Maximum number of naquadah bombs created')
end

function TOOL:LeftClick(trace)
	if(trace.Entity && trace.Entity:IsPlayer()) then 
        return false 
    end
	
    if(CLIENT) then 
        return true 
    end
    
	if(!self:GetSWEP():CheckLimit(entityName)) then 
        return false 
    end
    
	local player = self:GetOwner()
	local angle = trace.HitNormal:Angle()
   angle.pitch = angle.pitch + 90
   
	bomb = MakeBomb(self:GetClientInfo("detonationCode"),
					self:GetClientInfo("abortCode"),
                   self:GetClientNumber("yield"), 
                   self:GetClientNumber("chargeTime"), 
                   player, 
                   trace.HitPos, 
                   trace.HitNormal,
                   angle)
    
	if(trace.Entity:IsValid()) then
		weld = constraint.Weld(bomb, trace.Entity, 0, trace.PhysicsBone, 0, true)
		noCollide = constraint.NoCollide(bomb, trace.Entity, 0, trace.PhysicsBone)
		trace.Entity:DeleteOnRemove(bomb)
	else
		local bombPhysics = bomb:GetPhysicsObject()
		
      if(bombPhysics:IsValid()) then
			bombPhysics:Wake()
			bombPhysics:EnableMotion(true)
		end
	end
    
	undo.Create(entityName)
	undo.AddEntity(bomb)
    
	if(trace.Entity:IsValid()) then
		undo.AddEntity(weld)
		undo.AddEntity(noCollide)
	end
    
	undo.SetPlayer(player)
	undo.Finish()
	player:AddCleanup(entityName, bomb)
    
	return true
end

if(SERVER) then 
    CreateConVar("sbox_max"..entityName, 2)
    
	function MakeBomb(detonationCode, abortCode, yield, chargeTime, player, position, upVector, angle)
		if(!player:CheckLimit(entityName)) then 
            return nil 
        end
        
		local bomb = ents.Create(entityName)
      
      if(ValidEntity(bomb) == false) then
         error("Could not create "..entityName.."\n")
      end
      
      bomb:Setup(detonationCode, abortCode, yield, chargeTime)
      
      bomb:SetAngles(angle)
      bomb:SetPos(position + upVector * bomb:BoundingRadius())
      bomb:SetVar("Owner", player)
		bomb:Spawn()
        
		player:AddCount(entityName, bomb)
		return bomb
	end
end

function TOOL.BuildCPanel(panel)
	panel:AddControl('Header', {Text = "#Tool_"..entityName.."_name", Description = "#"..entityName})
   panel:AddControl("TextBox", 
   {
		Label = "Detonation Code",
		Description = "Enter the code you wish to use to detonate the bomb, NOTE: If you use anything other than numbers you will not be able to detonate via wire.",
		Command = entityName.."_detonationCode"
	})
	panel:AddControl("TextBox", 
   {
		Label = "Abort Code",
		Description = "Enter the code you wish to use to abort the detonation, NOTE: Using letters may result in not being able to trigger via wire",
		Command = entityName.."_abortCode"
	})
   panel:AddControl('Slider', {Label = 'Yield:', Type = "Integer", Min = 10, Max = 100, Command = entityName.."_yield"})
   panel:AddControl('Slider', {Label = 'Charge Duration:', Type = "Integer", Min = 4, Max = 300, Command = entityName.."_chargeTime"})
end
